package fr.web325.servlet.client.service;

import fr.web325.bean.Book;
import fr.web325.dao.ClientDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/shop/client/cate")
public class Category extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String category = req.getParameter("category");
        String currentPage,currentSize;
        if(req.getParameter("currentPage")==null){
            currentPage = "1";
            currentSize = "5";
        }else{
            currentPage = req.getParameter("currentPage");
            currentSize = req.getParameter("currentSize");
        }
        // 处理分类获取数据
        List<Book> bookList = null;
        try {
            bookList = new ClientDao().getBookListByCategory(category,Integer.parseInt(currentPage),Integer.parseInt(currentSize));
            String total = new ClientDao().getBookTotalByCategory(category);
            req.setAttribute("bookList",bookList);
            req.setAttribute("category",category);
            req.setAttribute("total",total);
            req.getRequestDispatcher("list.jsp").forward(req,resp);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
